package com.shouhuobao.take.parcel;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.shouhuobao.station.R;
import com.shouhuobao.take.model.ParcelBean;

/**
 * 包裹搜索
 *
 * @author coffee<br>
 * 2017年6月5日下午12:20:14
 */
public class ParcelSearchActivity extends BaseActivity2 implements OnClickListener {

    private EditText mSearchInput;
    private ListView mListview;
    private ParcelAdapter mAdapter;
    String keywords = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        keywords = getIntent().getStringExtra("keywords");
        showLoadingDialog(null);
        AppHttp.getInstance().takeParcelSearch(1, keywords);
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.take_parcel_search);
        super.findViewById();
        // getAppTitle().setCommonTitle("包裹搜索");
        mSearchInput = (EditText) findViewById(R.id.search_input);
        mListview = (ListView) findViewById(R.id.listview);
        findViewById(R.id.search_cancel).setOnClickListener(this);
        //
        mSearchInput.setText(keywords);
        mSearchInput.clearFocus();
        mSearchInput.setFocusable(false);
        mSearchInput.setFocusableInTouchMode(false);
        mSearchInput.setOnClickListener(this);
        mSearchInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    keywords = mSearchInput.getText().toString();
                    showLoadingDialog(null);
                    AppHttp.getInstance().takeParcelSearch(2, keywords);
                    return true;
                }
                return false;
            }
        });
        mAdapter = new ParcelAdapter(null, getContext());
        mListview.setAdapter(mAdapter);
        mListview.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(getContext(), ParcelDetailsActivity.class);
                intent.putExtra("item", (ParcelBean) parent.getItemAtPosition(position));
                startActivityForResult(intent, 101);
            }
        });
        //
        super.mSwipeLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                AppHttp.getInstance().takeParcelSearch(2, keywords);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //点击输入框获取焦点
            case R.id.search_input:
                mSearchInput.setFocusable(true);
                mSearchInput.setFocusableInTouchMode(true);
                mSearchInput.requestFocus();
                break;
            case R.id.search_cancel:
                finish();
                break;
        }
    }

    @Override
    public boolean handleMessage(final Message msg) {
        switch (msg.what) {
            case MsgID.take_parcel_search + 1:
                cancelLoadingDialog();
                onRefreshComplete();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ReqResult<ParcelBean> result1 = JSON.parseList(msg.obj, ParcelBean.class);
                        if (checkLoginStatus(result1)) {
                            mAdapter.getItems().clear();
                            mAdapter.appendItems(result1.getResultList(), true);
                        } else {
                            showToast(result1.getMessage());
                        }
                    }
                });
                return true;
            // 取走
            case MsgID.take_parcel_away:
                ReqResult<?> result = JSON.parser(msg.obj);
                if (checkLoginStatus(result)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // mAdapter.takeAwayNotify();
                        }
                    });
                } else {
                    showToast(result.getMessage());
                }
                return false;
            // 检索包裹--搜索结果
            case MsgID.take_parcel_search:
                cancelLoadingDialog();
                try {
                    final ReqResult<ParcelBean> result1 = JSON.parseList(msg.obj, ParcelBean.class);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (checkLoginStatus(result1)) {
                                mAdapter.setItems(result1.getResultList(), true);
                            } else {
                                showToast(result1.getMessage());
                            }
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
                return false;
            default:
                return super.handleMessage(msg);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        if (requestCode == 101) {
            AppHttp.getInstance().takeParcelSearch(2, keywords);
        }
    }
}
